﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SilverlightApplication.LanguageResources;
using System.Threading;
using LinqToAmazonSL;


namespace SilverlightApplication
{
    public partial class StatusBar : UserControl
    {
        private Boolean expanded; //Flag to specify when the status bar is shows the Amazon number field
        public static string amazonKey = string.Empty;
        public static string statusBar = string.Empty;

        public StatusBar()
        {
            InitializeComponent();
            expanded = false;
            stbMessage.Begin();
            txbAmazonKey.Text = LanguageLoader.GetString("txbAmazonKey");
            AmazonKey.Text = LanguageLoader.GetString("AmazonKey");
            tbkPageState.Text = LanguageLoader.GetString("NoKeyMsg");
            String value = LanguageLoader.GetString("toolTipstatusBar");
            cnvCircle.SetValue(ToolTipService.ToolTipProperty, value);
           
        }

        private void cnvCircle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!expanded)
            {
                expanded = true;
                stbShowBar.Begin();
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            expanded = false;
            stbHideBar.Begin();
            amazonKey = txbAmazonNumber.Text;
            Dispatcher.BeginInvoke(() =>
            {
                    tbkPageState.Text = LanguageLoader.GetString("Validating");
                    statusBar = "Validating...";
            });
           
            Thread queryThread = new Thread(LinQueryValidate);
            queryThread.Start();
            
            stbHideBar.Begin();
        }

        void LinQueryValidate()
        {
            AmazonQueryProvider<AmazonBook> provider = new AmazonQueryProvider<AmazonBook>(new AmazonBookQueryCriteria(), amazonKey);
            bool validate = provider.ValidateKey();
              Dispatcher.BeginInvoke(() =>
                    {
                        if (validate)
                        {
                            tbkPageState.Text = LanguageLoader.GetString("Ready");
                            statusBar = "Ready";
                        }
                        else
                        {
                            tbkPageState.Text = LanguageLoader.GetString("InvalidKey");
                            statusBar = "Invalid Key";
                        }
                        });
                
        }
    }
}
